<?php
class ObjectController extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'location', '', TRUE );
		$this->load->model ( 'object', '', TRUE );
		$this->load->model ( 'photo', '', TRUE );
		$this->load->model ( 'type', '', TRUE );
	}
	function addObject() {
		$id_User = $_POST ['id_User'];
		$name = $_POST ['name'];
		$id_Type = $_POST ['type'];
		$address = $_POST ['address'];
		$url = $_POST ['url'];
		$latitude = $_POST ['latitude'];
		$longitude = $_POST ['longitude'];
		$image = $_POST ['image'];
		$id_Location = $this->location->addLocation ( $latitude, $longitude );
		//$id_Location = $result1->id_Location;
		

		//$id_Photo = $result2->id_Photo;
		/*	$query = $this->type->getType ( $type );
		if ($query) {
			foreach ( $query as $row ) {
				$id_Type = $row->id_Type;
			}
		}*/
		$id_Obj = $this->object->addObject ( $id_User, $name, $id_Type, $address, $url, $id_Location );
		//print $result;
		$id_Photo = $this->photo->addPhoto ( $id_User, $id_Obj, $image );
		print $id_Photo;
	}
	function addBasicObject() {
		$name = $_POST ['name'];
		$id_User = $_POST ['id_User'];
		$id_Type = $_POST ['id_Type'];
		//$id_Location = $this->location->addLocation ( 0, 0 );
		$id_Obj = $this->object->addObject ( $id_User, $name, $id_Type, '', '', '' );
		print $id_Obj;
	}
	
	function updateObject() {
		$id_User = $_POST ['id_User'];
		
		$id_Obj = $_POST ['id_Obj'];
		$address = $_POST ['address'];
		$url = $_POST ['url'];
		$latitude = $_POST ['latitude'];
		$longitude = $_POST ['longitude'];
		$image = $_POST ['image'];
		/*$id_User =2;
		$name = 'cheÌ Bobo';
		$id_Obj = 8;
		$address = 'hanoi';
		$url = '';
		$latitude = 0.0;
		$longitude = 0.0;
		$image = '';*/
		
		if ($latitude != 0 && $longitude != 0) {
			$id_Location = $this->location->addLocation ( $latitude, $longitude );
		} else
			$id_Location = '';
		$result = $this->object->updateObject ( $id_User, $id_Obj, $address, $url, $id_Location );
		//print $result;
		if ($result) {
			if ($image != '') {
				$id_Photo = $this->photo->addPhoto ( $id_User, $id_Obj, $image );
				print $id_Photo;
			} else
				print $id_Obj;
		} else
			print '0';
	}
	function addHotel() {
		$name = $_POST ['name'];
		$id_User = $_POST ['id_User'];
		
		$id_Location = $this->location->addLocation ( 0, 0 );
		$id_Obj = $this->object->addObject ( $id_User, $name, 1, '', '', $id_Location );
		print $id_Obj;
	}
	function addRes() {
		$name = $_POST ['name'];
		$id_User = $_POST ['id_User'];
		$id_Location = $this->location->addLocation ( 0, 0 );
		$id_Obj = $this->object->addObject ( $id_User, $name, 2, '', '', $id_Location );
		print $id_Obj;
	}
	function addEvent() {
		$name = $_POST ['name'];
		$id_User = $_POST ['id_User'];
		$id_Location = $this->location->addLocation ( 0, 0 );
		$id_Obj = $this->object->addObject ( $id_User, $name, 3, '', '', $id_Location );
		print $id_Obj;
	}
	
	function getObjectById() {
		$id = $_POST ['id_Obj'];
		//$id=array(1,5,7);
		$result = $this->object->getObject ( $id );
		print (json_encode ( $result )) ;
	}
	function getObjectByIdLocation() {
		$id_Location = $_POST ['id_Location'];
		//$id=array(1,5,7);
		$result = $this->object->getObjectByLocation ( $id_Location );
		print (json_encode ( $result )) ;
	}
	function getAllObjectByType() {
		$type = $_POST ['id_Type'];
		$result = $this->object->getAllObjectByType ( $type );
		print (json_encode ( $result )) ;
	}
	function getObjectByName() {
		$name = $_POST ['name'];
		$result = $this->object->getObjectByName ( $name );
		print (json_encode ( $result )) ;
	
	}

}